jQuery(document).ready(function () {
    var user, chart1, option1,chart2,option2;

    $.ajax({
        url: "",
        type: "POST",
        dataType: "json",
        success: function (data) {

        }
    });

    $.ajax({
        url: "/solution/analyzeresult",
        type: "POST",
        async: true,
        dataType: "json",
        success: function (data) {
            $("#num-solve")
            var data11=[];
            $.each(data.submit,function (n,v) {
                data11.push({name:v.md,value:[v.md,v.num]});
            });
            var data12=[];
            $.each(data.ac,function (n,v) {
                data12.push({name:v.md,value:[v.md,v.num]});
            });
            chart1 = echarts.init(document.getElementById('chart1'));
            option1 = {
                title: {
                    text: '每日提交/AC量'
                },
                tooltip : {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross',
                        label: {
                            backgroundColor: '#6a7985'
                        }
                    }
                },
                legend: {
                    data:['提交量','AC量']
                },
                toolbox: {
                    feature: {
                        saveAsImage: {}
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis : [
                    {
                        type : 'time'
                    }
                ],
                yAxis : [
                    {
                        type : 'value'
                    }
                ],
                series : [
                    {
                        name:'提交量',
                        type:'line',
                        stack: '提交量',
                        areaStyle: {normal: {}},
                        data:data11
                    },
                    {
                        name:'AC量',
                        type:'line',
                        stack: 'AC量',
                        areaStyle: {normal: {}},
                        data:data12
                    }
                ]
            };
            chart1.setOption(option1);
            var resultName=["等待","等待重判","编译中","运行并评判","正确","格式错误","答案错误","时间超限","内存超限","输出超限","运行错误","编译错误","编译成功","运行完成"];
            var data21=[];
            var data22=[];
            $.each(data.result,function (n,v) {
                data21.push(resultName[v.result]);
                data22.push({name:resultName[v.result],value:v.num});
            });
            chart2 = echarts.init(document.getElementById('chart2'));
            option2 = {
                title : {
                    text: '提交结果分析',
                    x:'center'
                },
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    type: 'scroll',
                    orient: 'vertical',
                    right: 10,
                    top: 20,
                    bottom: 20,
                    data: data21,
                    selected:data21
                },
                series : [
                    {
                        name: '提交结果',
                        type: 'pie',
                        radius : '55%',
                        center: ['40%', '50%'],
                        data: data22,
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            chart2.setOption(option2);
        }
    });


    //金钱
    $("#table-money").dataTable({
        "processing": true,
        "serverSide": true,
        "paging": true,
        lengthMenu: [
            [10, 20, 50, 100],
            ['10', '20', '50', '100']
        ],
        ordering: false,
        "ajax": {
            "url": "/user/moneylog",
            "type": "POST",
            "dataType": "json"
        },
        "searching": true,
        "columns": [
            {"data": "reason"},
            {"data": "changelog"},
            {"data": "time"}
        ],
        "oLanguage": {
            "sProcessing": "正在获取数据，请稍后...",
            "sLengthMenu": "显示 _MENU_ 条",
            "sZeroRecords": "没有找到数据",
            "sInfo": "从 _START_ 到  _END_ 条记录 总记录数为 _TOTAL_ 条",
            "sInfoEmpty": "记录数为0",
            "sInfoFiltered": "(全部记录数 _MAX_ 条)",
            "sInfoPostFix": "",
            "sSearch": "查询",
            "sUrl": "",
            "oPaginate": {
                "sFirst": "第一页",
                "sPrevious": "上一页",
                "sNext": "下一页",
                "sLast": "最后一页"
            }
        }
    });

    //基本信息
    $.ajax({
        url: "/user/basicinfo",
        type: "POST",
        async: true,
        dataType: "json",
        success: function (data) {
            user = data;
            // 用户信息开始
            $("#user-name").html(data.nickname);
            if (data.stage) {
                $("#user-level").html("Lv." + data.userInfo.level);
                $("#user-exp").html(data.userInfo.exp);
                $("#user-ch").html(data.userInfo.title);
            } else {
                $("#user-level").html("无");
                $("#user-exp").html("无");
                $("#user-ch").html("无称号");
            }
            $("#user-money").html(data.userInfo.money);
            // 用户信息结束
            $("#btn-email").html(data.email);
            $("#btn-nickname").html(data.nickname);
            $("#btn-qq").text(data.qq);
            $("#btn-sno").text(data.studentId);
        }
    });

    //工具提示
    $("[data-toggle='tooltip']").tooltip();

    $("#btn-nickname").click(function () {
        $("#btn-nickname").hide();
        $("#hiden-nickname").html("<input type=\"text\" name=\"nickname\" placeholder=\"\" class=\"form-control \" id=\"form-profile-nickname\">\n" +
            "                            <span class=\"input-group-btn\">\n" +
            "                                <button class=\"btn btn-default\" type=\"button\" id=\"btn-submit-nickname\">提交</button>\n" +
            "                            </span>");
    });
    $("#btn-qq").click(function () {
        $("#btn-qq").hide();
        $("#hiden-qq").html("<input type=\"text\" name=\"qq\" placeholder=\"\" class=\"form-profile-item form-control\" id=\"form-profile-qq\">\n" +
            "                            <span class=\"input-group-btn\">\n" +
            "                                <button class=\"btn btn-default\" type=\"button\" id=\"btn-submit-qq\">提交</button>\n" +
            "                            </span>").after(btnQqClick());
    });
    $("#btn-sno").click(function () {
        $("#btn-sno").hide();
        $("#hiden-sno").html("<input type=\"text\" name=\"sno\" placeholder=\"\" class=\"form-profile-item form-control\" id=\"form-profile-sno\">\n" +
            "                            <span class=\"input-group-btn\">\n" +
            "                                <button class=\"btn btn-default\" type=\"button\" id=\"btn-submit-sno\">提交</button>\n" +
            "                            </span>").after(btnSnoClick());
    });

    //更改密码
    $('#form-password').bootstrapValidator({
        message: "该值无效",
        fields: {
            password_old: {
                message: '原密码无效',
                validators: {
                    notEmpty: {
                        message: '原密码不能为空'
                    },
                    stringLength: {
                        min: 6,
                        message: "原密码长度不能低于6位"
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: '原密码只能由字母、数字、点、下划线组成 '
                    },
                    different: {
                        field: 'password_old',
                        message: "新密码不能与原密码相同"
                    }
                }
            },
            password_new: {
                validators: {
                    notEmpty: {
                        message: "新密码不能为空"
                    },
                    stringLength: {
                        min: 6,
                        message: "新密码长度最少为6位"
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: '新密码只能包含数字,字母,点和下划线'
                    },
                    identical: {
                        field: 'password_new2',
                        message: "两次新密码输入不一致"
                    },
                    different: {
                        field: 'password_old',
                        message: "新密码不能与原密码相同"
                    }
                }
            },
            password_new2: {
                validators: {
                    notEmpty: {
                        message: "新密码不能为空"
                    },
                    stringLength: {
                        min: 6,
                        message: "新密码长度最少为6位"
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: '新密码只能包含数字，点和下划线'
                    },
                    identical: {
                        field: 'password_new',
                        message: "两次新密码输入不一致"
                    },
                    different: {
                        field: 'password_old',
                        message: "新密码不能与原密码相同"
                    }
                }
            }
        }
    });
    $("#submit-password").click(function () {
        var form = $('#form-password');
        var bv = form.data('bootstrapValidator');
        bv.validate();
        if (bv.isValid()) {
            $.ajax({
                url: '/user/updatepassword',
                async: true,
                type: 'POST',
                data: form.serialize(),
                success: function (data) {
                    if (data.ok) {
                        $("#changepassword").modal('hide');
                        toastr.options = {
                            "closeButton": false,
                            "debug": false,
                            "positionClass": "toast-top-center",
                            "onclick": null,
                            "showDuration": "1000",
                            "hideDuration": "1000",
                            "timeOut": "2000",
                            "extendedTimeOut": "1000",
                            "showEasing": "swing",
                            "hideEasing": "linear",
                            "showMethod": "fadeIn",
                            "hideMethod": "fadeOut"
                        };
                        toastr["info"]("密码修改成功", "操作成功");
                    } else {
                        toastr.options = {
                            "closeButton": false,
                            "debug": false,
                            "positionClass": "toast-top-center",
                            "onclick": null,
                            "showDuration": "1000",
                            "hideDuration": "1000",
                            "timeOut": "2000",
                            "extendedTimeOut": "1000",
                            "showEasing": "swing",
                            "hideEasing": "linear",
                            "showMethod": "fadeIn",
                            "hideMethod": "fadeOut"
                        };
                        toastr["error"](data.msg, "操作失败");
                    }
                }
            })
        }
    });
    $('#form-password').keydown(function (e) {
        if (e.keyCode == 13) {
            $("#submit-password").click();
        }
    });


    function btnSnoClick() {
        $("#hiden-sno button").click(function () {
            $.ajax({
                url: "/user/updatesid",
                type: "POST",
                data: {"sid": $("#form-profile-sno").val()},
                dataType: "json",
                success: function () {
                    $("#form-profile-sno").hide();
                    $("#btn-submit-sno").hide();
                    $("#btn-sno").text($("#form-profile-sno").val())
                    $("#btn-sno").show();
                }
            })
        })
    }

    function btnQqClick() {
        $("#hiden-qq button").click(function () {
            $.ajax({
                url: "/user/updateqq",
                type: "POST",
                data: {"qq": $("#form-profile-qq").val()},
                dataType: "json",
                success: function () {
                    $("#form-profile-qq").hide();
                    $("#btn-submit-qq").hide();
                    $("#btn-qq").text($("#form-profile-qq").val());
                    $("#btn-qq").show();
                }
            })
        })
    }
})